Skip to main content Skip to complementary content

Sample code for how to connect to Qlik Sense

There are several ways to connect to Qlik Sense through the Qlik Sense .NET SDK, depending on how you want to interact with your applications and expand your Qlik Sense solution. To get started and connect to the Qlik Sense installation using the .NET SDK, see Getting started with the Qlik Sense .NET SDK.

The best way to connect depends on your environment. The purpose of the sample code provided by Qlik is to demonstrate capabilities and use cases of the API.

We recommend that you download and run the appropriate example before you continue reading. All the sample code for the connection examples are available on Github.

Information note The purpose of the sample code provided by Qlik is to demonstrate capabilities and use cases of the API. They are provided on an as-is basis, without any guarantees that they will work in all system configurations and future software releases. These code samples are released under: Wikipedia: MIT license.

Set location to the Qlik Sense instance

The connection to Qlik Sense is established by using a URI.

QMC - Proxies

  • Encrypted, https://server.domain.com / wss://server.domain.com.

    
    	ILocation location = Qlik.Engine.Location.FromUri(new Uri("https://server.domain.com"));
  • Non encrypted, http://server.domain.com / ws://server.domain.com.

Authentication

Decide what type of security you want to authenticate with.

Security options for authentication
Environment Connection method
Working on a local machine. Only http or ws.

Connect to Qlik Sense Desktop.

Working in a Windows network. You always connect as the logged on user. The computer must be on a Windows network (NT Lan Manager) or compatible in order to connect to Qlik Sense Server with NTLM.

Connect to Qlik Sense Server with NTLM.

Communicating between secure servers.

Connect to Qlik Sense Server that is configured with static header.

Developing a Windows service that will run on the same computer as Qlik Sense. You connect directly through engine, not through the proxy, and always through port 4747. Connect directly to a local Qlik Sense with a certificate.
Working outside a Windows network. You always connect as the logged on user. Connect to Qlik Sense Server with NTLM and Network Credentials.
When you have a ticket and want to reuse it in your connection. In this case you always connect through the proxy.

Connect to Qlik Sense with an existing session ticket.

For a public website. Connect to Qlik Sense as an anonymous user.

Developing a Windows service that will run on another computer than Qlik Sense. You connect directly through engine, not through the proxy, and always through port 4747.

Connect directly to a remote Qlik Sense using a certificate.

Connection mode and set user

  • Qlik Sense Desktop

    For Qlik Sense Desktop, use the AsDirectConnectionToPersonalEdition method in order to set up security mode. This means that the location is defined as a direct connection to Qlik Sense Desktop.

    Example:  

    
    	location.AsDirectConnectionToPersonalEdition();
  • Qlik Sense Server with NTLM.

    The server version of Qlik Sense only supports the connection AsNtlmUserViaProxy method. The AsNtlmUserViaProxy method takes a parameter to indicate if we are communicating encrypted or not. The default value for proxyUsesSsl is true. If connecting with http or ws set proxyUsesSsl:false. All communication will be executed as the user logged on.

    If you want to impersonate see, the Qlik Sense Server with NTLM and Network Credentials example.

    Example:  

    
    	location.AsNtlmUserViaProxy(proxyUsesSsl:true);
  • Qlik Sense Server that is configured with static header.

    Add user name and header authentication key. Check your virtual proxy configuration in the QMC. The user name in the provided example code is, X-Qlik-HeaderAuth.

    Example:  

    
    	location.AsStaticHeaderUserViaProxy("username", "X-Qlik-HeaderAuth");

    The Header authentication header name must match the name set under Virtual proxy editin the QMC. The user name has to be under Users in the QMC.

    Example: How to set the virtual proxy

    
    	location.VirtualProxyPath = "static";
  • Directly to a local Qlik Sense with a certificate.

    The server version of Qlik Sense only supports the connection AsDirectConnection method.

    Example:  

    
    	location.AsDirectConnection("domain", "user");
  • Qlik Sense Server with NTLM and Network Credentials.

    The server version of Qlik Sense only supports the connection AsNtlmUserViaProxy method. The AsNtlmUserViaProxy method takes a parameter to indicate if we are communicating encrypted or not. The default value for proxyUsesSsl is true. If connecting with http or ws set proxyUsesSsl:false. All communication will be executed with the network credentials.

    Example:  

    
    	ILocation location = Qlik.Engine.Location.FromUri(uri);
    				​
    	var domain = "myDomain";
    	var user = "myUser";
    	SecureString pwd  = new SecureString();
    	pwd.AppendChar('p');
    	pwd.AppendChar('a');
    	pwd.AppendChar('s');
    	pwd.AppendChar('s');
    	pwd.AppendChar('w');
    	pwd.AppendChar('o');
    	pwd.AppendChar('r');
    	pwd.AppendChar('d');
    				​
    	location.AsNtlmUserViaProxy(proxyUsesSsl: uri.Scheme.Equals(Uri.UriSchemeHttps), loginCredentials: new NetworkCredential(user, pwd, domain));
  • Qlik Sense with an existing session ticket.

    Use the connection method AsExistingSessionViaProxy. The ExtractTicketFromCookies illustrates a way to get the session ticket that is needed for this connection method. In this example we assume the session ticket is stored in a cookie. For more information, see QMC - Virtual proxies.

    Example:  

    
    	// Defining the location as an existing session to Qlik Sense Server
    	location.AsExistingSessionViaProxy(sessionCookie.Value, sessionCookie.Name);
    
    	///<summary> 
    	/// This method is an example on how a session cookie can be extracted.
    	/// It opens up a connection towards Qlik Sense Server on given 
    	/// uri and extracts the session id 'X-Qlik-Session' from the response cookies
    	///<summary> 
    	///<returns>session cookie<returns>
    	private static Cookie ExtractTicketFromCookies()
    	{
    		var _qlikSenseServerUri = new Uri("https://server.domain.com");
    
    		CookieContainer cookieContainer = new CookieContainer();
    		var connectionHandler = new HttpClientHandler
    		{
    			UseDefaultCredentials = true,
    			CookieContainer = cookieContainer
    		};
    		var connection = new HttpClient(connectionHandler);
    
    		connection.DefaultRequestHeaders.Add("X-Qlik-xrfkey", "ABCDEFG123456789");
    		connection.DefaultRequestHeaders.Add("User-Agent", "Windows");
    
    		connection.GetAsync(_qlikSenseServerUri).Wait();
    
    		IEnumerable<Cookie> responseCookies = cookieContainer.GetCookies(_qlikSenseServerUri).Cast<Cookie>();
    
    		return responseCookies.First(cookie => cookie.Name.Equals("X-Qlik-Session"));
    	}
    				
  • Qlik Sense as an anonymous user.

    The server version of Qlik Sense only supports the connection AsAnonymousUserViaProxy method. For more information about configuring the QMC to accept anonymous users, see Anonymous authentication.

    Example:  

    
    	// Defining the location as using anonymous mode i.e. not using a specific user when establishing connection
    	location.AsAnonymousUserViaProxy();
    				
  • Directly to a remote Qlik Sense with a certificate.

    The server version of Qlik Sense only supports the connection AsDirectConnection method. In this example the certificate is stored in the file client.pfx, which has been exported from the QMC. For more information, see Export certificates.

    Example:  

    
    	X509Certificate2 x509 = new X509Certificate2();
    	//Create X509Certificate2 object from .cer file.
    	byte[] rawData = File.ReadAllBytes(@"c:\cert\client.pfx");
    	x509.Import(rawData, "Qlik2Run", X509KeyStorageFlags.UserKeySet);
    	X509Certificate2Collection certificateCollection = new X509Certificate2Collection(x509);
    	// Defining the location as a direct connection to Qlik Sense Server
    	location.AsDirectConnection("domain", "user", certificateCollection:certificateCollection);			

Did this page help you?

If you find any issues with this page or its content – a typo, a missing step, or a technical error – let us know how we can improve!